package cfdt.common.utils.minio;

import com.alibaba.nacos.common.utils.IoUtils;
import io.minio.*;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

@Component
public class MinioUtils {

    public static String url;

    public static String accessKey;

    public static String secretKey;
    
    public static MinioClient minioClient;

    @Value("${minio.url:#{null}}")
    public void setUrl(String url) {
        MinioUtils.url = url;
    }

    @Value("${minio.accesskey:#{null}}")
    public void setAccessKey(String accessKey) {
        MinioUtils.accessKey = accessKey;
    }

    @Value("${minio.secretkey:#{null}}")
    public void setSecretKey(String secretKey) {
        MinioUtils.secretKey = secretKey;
    }

    public static synchronized void init() {
        if (minioClient == null) {
            OkHttpClient okHttpClient = new OkHttpClient.Builder().connectionPool(new ConnectionPool(500, 10, TimeUnit.MINUTES)).build();
            minioClient = MinioClient.builder().endpoint(url).httpClient(okHttpClient).credentials(accessKey, secretKey).build();
            
//            minioClient = MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();
        }
    }

    // 通过流上传一个文件 MultipartFile 只要在controller接收就好,是前端发过来的,
    // myBucketname 是文件存放在那个文件夹(桶)里面
    // multipartFile.getOriginalFilename() 这个是文件的名字 .
    public static String uploadFile(String bucketName, String objectName, byte[] objectContent) throws Exception {
        if (minioClient == null) {
            init();
        }
        if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()))
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());

        return minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName)
                .stream(new ByteArrayInputStream(objectContent), objectContent.length, -1).build()).toString();
    }

    // 通过文件名 和 文件夹(桶) 名字删除一个对象
    public static void deleteFile(String bucketName, String objectName) throws Exception {
        if (minioClient == null) {
            init();
        }
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
    }

    // 文件下载
    public static byte[] downloadFile(String bucketName, String objectName) throws Exception {
        if (minioClient == null) {
            init();
        }
        try (InputStream inputStream = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();) {
            IoUtils.copy(inputStream, outputStream);
            return outputStream.toByteArray();
        }
    }
}